<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use app\common\library\Token;
use app\common\model\LitestoreShop;
use app\common\model\UserCollectShop;
use think\Config;
use think\Cookie;

class Shop extends Frontend
{

    protected $noNeedLogin = '*';

    /**
     * $id
     * 商店首页
     */
    public function index($id = null)
    {
        if(!$id) $this->error('参数错误');
        $LitestoreShop = new LitestoreShop();
        //xss攻击过滤
        $id=remove_xss($id);
        //防注入过滤
        $id=filterWords($id);
        $shop = $LitestoreShop->where('id',$id)->find();
        if(!$shop) $this->error('商户不存在');
        if($shop['status'] == 2) $this->error('商户已关闭');
        if($shop['status'] == 0) $this->error('商户审核中');
        $shop['shop_images'] = explode(',',$shop['shop_images']);
        @$length = web_getDistance($this->location['content']['point']['x'],$this->location['content']['point']['y'],$shop['longitude'],$shop['latitude']);
        $this->assign('shop',$shop);
        $this->assign('length',$length);

        //有没有收藏
        $UserCollectShop = new UserCollectShop();
        $cmap['user_id'] = Cookie::get('uid');
        $cmap['shop_id'] = $id;
        $is_collect = $UserCollectShop->where($cmap)->find();

        $this->assign('is_collect',$is_collect);

        return $this->fetch();

    }

    /**
     * 评论列表
     */
    public function shop_comment(){
        //评论列表
        $LitestoreShop = new LitestoreShop();
        $page=$this->request->get('page',1);
        //商铺id
        $map['s.id'] = $this->request->get('id',1);
        //xss安全过滤
        $map['s.id']=remove_xss($map['s.id']);
        //防注入过滤
        $map['s.id']=filterWords($map['s.id']);

        $list = db('litestore_shop')->alias('s')
                ->field('u.username,c.*')
                ->join('__LITESTORE_SHOPORDER__ o','o.litestore_shop_id = s.id')
                ->join('__LITESTORE_SHOPCOMMENT__ c','c.litestore_shoporder_id = o.shoporder_id')
                ->join('__USER__ u','u.id = o.playUser_id')
                ->where($map)
                ->page($page)->limit(10)->select();
        if(!$list) exit;
        $this->assign('list',$list);
        return $this->fetch();
    }

    /**
     * 加载附近店铺
     */
    public function near()
    {
        return $this->fetch();
    }

    /**
     * 加载店铺列表模块
     */
    public function shoplist_ajax()
    {
        $LitestoreShop = new LitestoreShop();
        //店铺搜索关键词
        $keysword = $this->request->request('keysword');
        //xss攻击过滤
        $keysword=remove_xss($keysword);
        //防注入过滤
        $keysword=filterWords($keysword);

        //店铺分类id
        $category_id = $this->request->request('category_id');
        //xss攻击过滤
        $category_id=remove_xss($category_id);
        //防注入过滤
        $category_id=filterWords($category_id);

        if($category_id){
            $map['category_id']=$category_id;
        }
        if($keysword){
            $map['name']=['like','%'.$keysword.'%'];
        }

        $map['status'] =1;
        //经度
        if(!empty($this->location['content'])){
            $y=$this->location['content']['point']['y'];
        }else{
            $y=22.646479;
        }
        //维度
        if(!empty($this->location['content'])){
            $x=$this->location['content']['point']['x'];
        }else{
            $x=114.045094;
        }

        $field = 'average,id,name,shop_image,longitude,latitude,category_id,';
        $field .= '( 6371.137 * acos (cos(radians('.$y.'))*cos( radians( s.latitude ))*cos( radians(s.longitude )-radians('.$x.'))+sin(radians('.$y.'))*sin(radians(s.latitude)))) AS distance';
        $order="distance asc";
        $page = $this->request->get('page',1);
        $data = $LitestoreShop->getList($map,$order,$field,$page);
        foreach ($data as $k=>$v){
            @$data[$k]['length'] = web_getDistance($this->location['content']['point']['x'],$this->location['content']['point']['y'],$v['longitude'],$v['latitude']);
        }
        if(!$data) {
            echo 0;exit;
        }
        $this->assign('data',$data);
        return $this->fetch();
    }

    /**
     * @param $id
     * @param $type
     * 收藏/取消收藏店铺
     */
    public function collect_shop($id,$type)
    {
        $UserCollectShop = new UserCollectShop();
        $res = $UserCollectShop->collect_shop(Cookie::get('uid'),$id,$type);
        if($res == 1){
            if($type==1){
                $this->success('收藏成功');
            }elseif($type==2){
                $this->success('取消收藏');
            }

        }elseif($res == -1){
            $this->error('已添加不许重复添加');
        }elseif($res == -2){
            $this->error('不存在无法删除');
        }else{
            $this->error('操作失败，请重试');
        }
    }

    /**
     * 搜索
     */
    public function search()
    {

        return $this->fetch();
    }


}
